<?php
	/**
	 *	Wap Portal Server
	 *
	 *	Copyright (C) 2002 - 2004 Emir Sakic
	 *
	 *	Wap Portal Server
	 *	Developer: Emir Sakic - saka@hotmail.com
	 *  Homepage: http://www.sakic.net
	 *	Date: 15/03/2004
	 * 	Version #: 1.2
	**/

// Add to database
if (isset($link_id)) {
	if (isset($published) && $published == 'Yes') {
		$published = 1;
	} else {
		$published = 0;
	}

	// Replace quotes
	if (!get_magic_quotes_gpc()) $linktitle = str_replace("'","\'",$linktitle);

	if ($link_id == 'New') {
		$query = "INSERT INTO wps_links VALUES ( '', '$linkorder', '$linktitle', '$linkcontent', '$published')";
	} else {
		$query = "UPDATE wps_links SET ordering = '$linkorder', linktext = '$linktitle', link = '$linkcontent', published = '$published' WHERE id = '$link_id'";
	}
		$result = mysql_query($query)
	    	or die("Insert failed");
	$link_id = 0;
}

// Delete from database
if (isset($del_id)) {
	$query = "DELETE FROM wps_links WHERE id = '$del_id'";
	mysql_query($query) or die("Delete failed");
	$del_id = 0;
}
?>
<script language="JavaScript">
<!--
function validate(){
	if ((document.form.linktitle.value=="") || (document.form.linkcontent.value=="")){
		alert('Please complete the form!');
		return false;
		}
	else {
		return true;
		}
	}
//-->
</script>

  <table width="600">
    <tr>
      <td colspan="3"><a href='index.php' border=0>Home</a> &gt; Links Management</td>
    </tr>
    <tr>
      <td align="center" width="25%"><b>Manage&nbsp;Links</b></td>
      <td align="center" width="50%"><b>Add/Edit Links</b></td>
	  <td align="center" width="25%"><b>Resources</b></td>
    </tr>
    <tr>
      <td valign="top">
        <?// List existing link titles
print "<a href=\"index.php?option=links\">Add Link</a><br><br>\n";
$query = "SELECT * FROM wps_links ORDER BY ordering";
$result = mysql_query($query);
// Use mysql_fetch_row to display links
for ($count = 1; $row = mysql_fetch_row ($result); ++$count)
{
	print "        <a href=\"index.php?option=links&linkid=$row[0]\">$row[2]</a><br>\n";
}?>
      </td>
      <td>
        <?// Read link from mySQL database
if (isset($linkid)) {
	$query = "SELECT * FROM wps_links WHERE id = '$linkid'";
	$result = mysql_query($query);
	$row = mysql_fetch_row($result);
}
?>
        <form name="form" method="post" action="index.php?<? echo $_SERVER['QUERY_STRING']; ?>" onSubmit="return validate()">
          <p> <b>Title:</b><br>
            <input type="text" name="linktitle" value="<? if (isset($linkid)) { echo $row[2]; } ?>" size="15">
            <br>
            <b>Address:</b><br>
            http://
            <input type="text" name="linkcontent" value="<? if (isset($linkid)) { echo $row[3]; } ?>" size="30">
          </p>
          <p>Ordering:
            <?
// Count them
$query = "SELECT * FROM wps_links";
$result = mysql_query($query);
for ($num=1; $row = mysql_fetch_row ($result); ++$num) {
}
print "          <select name=\"linkorder\">\n";
if (isset($linkid)) {
	$query = "SELECT * FROM wps_links WHERE id = '$linkid'";
	$result = mysql_query($query);
	$row = mysql_fetch_row ($result);
	for ($count=1; $count <> $num; ++$count) {
		if ($row[1] == $count) {
			print "              <option selected>$count</option>\n";
		} else {
			print "              <option>$count</option>\n";
		}
	}
} else {
	for ($count=1; $count <> $num; ++$count) {
		print "              <option>$count</option>\n";
	}
	print "              <option selected>$count</option>\n";
}
print "            </select>\n";
			?>
          </p>
          <p>Published:
            <?
if (isset($linkid)) {
	$query = "SELECT * FROM wps_links WHERE id = '$linkid'";
	$result = mysql_query($query);
	$row = mysql_fetch_row($result);
}?><input type="checkbox" name="published" value="Yes"<? if (empty($linkid) || $row[4]==1) echo " checked"; ?>></p>
          <input type="hidden" name="link_id" value="<? if (isset($linkid)) { echo $row[0]; } else { echo 'New'; } ?>">
          <input type="submit" name="Submit" value="Submit">
        </form>
        <? if (isset($linkid)) {
		print "<form name=\"delete\" method=\"post\" action=\"index.php?option=links\">
		<input type=\"hidden\" name=\"del_id\" value=\"$row[0]\">
		<input type=\"submit\" name=\"delete\" value=\"Delete\">
		</form>";
		} ?>
      </td>
      <td valign="top"><b>Title:</b><br>
        Text that appears as link<br>
        <b>Address format:</b><br>
        wap.site.com/<br>
        <br>
        <br>
<?php
	$common->setup_default_emulator();
	$common->display_emulator();
?>
      </td>
    </tr>
  </table>
